home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / LDynamicPopupMenu / LDynamicPopupMenu.cp < prev    next >
Encoding:
Text File  |  1995-01-08  |  5.3 KB  |  184 lines  |  [TEXT/CWIE]

  1. // HuePopup.cp
  2. //        written by Constantine Spathis 
  3. //            [see LDynamicPopupMenu.h for more info]
  4. //
  5. //        1/8/95 CS
  6. //            Initial Iteration
  7.  
  8. //    Class Specific Headers
  9. #include "LDynamicPopupMenu.h"
  10.  
  11. // Macintosh Headers
  12. #include <Menus.h>
  13.  
  14. //    ANSI Header
  15. #include <string.h>    //    Only included for memcpy.                    
  16.  
  17. // Local Global
  18. int s_numMenu=20000;//    Where to start MenuID's for the new menu's created. This must
  19.                     //    be far away from other menus so they don't clash. Also since
  20.                     //    MenuID's used in the NewMenu call are shorts we want a large
  21.                     //    number of useful id's.
  22.  
  23.  
  24. //    Local Function Definitions
  25. void c2pstrcpy(const char* in_pStr,StringPtr out_pStr);
  26.  
  27.  
  28. //    LDynamicPopupMenu(
  29. //            LStream *in_pStream);
  30. //        12/13/94 CS - Stream based constructor
  31. LDynamicPopupMenu::LDynamicPopupMenu(
  32.     LStream* in_pStream)    //    input stream
  33.     : LStdPopupMenu(in_pStream)
  34. {
  35.     ;
  36. }
  37. //    static LDynamicPopupMenu* CreateStdPopupMenuStream(
  38. //            LStream *in_pStream);
  39. //        1/8/95 CS
  40. //            The idea is to get rid of PowerPlants control and create our own.
  41. LDynamicPopupMenu*
  42. LDynamicPopupMenu::CreateLDynamicPopupMenuStream(
  43.     LStream* in_pStream)    //    input stream
  44. {    
  45.     LDynamicPopupMenu *out_pMenu=new LDynamicPopupMenu(in_pStream);
  46.     OSErr    err=noErr;
  47.     
  48.     //    Get old menu title    
  49.     Str255    sMenuTitle;
  50.     memcpy(sMenuTitle,(**(out_pMenu->mMacControlH)).contrlTitle,
  51.                     *((**(out_pMenu->mMacControlH)).contrlTitle)+1);
  52.     
  53.     //    Get old menu Control Refcon
  54.     long lRefCon=GetCRefCon(out_pMenu->mMacControlH);
  55.     
  56.     //    Dispose of the control PowerPlant made.
  57.     DisposeControl(out_pMenu->mMacControlH);
  58.  
  59.     //    Insert a new menu for this control exclusive use.
  60.     MenuHandle hTempMenu=NewMenu(s_numMenu,sMenuTitle);
  61.     
  62.     //    Here you really should add some code to check that menu was properly made
  63.     //    because NewMenu can fail. Just a suggestion :-)
  64.     
  65.     //    xassert(ValidHandle(Handle(hTempMenu)));
  66.  
  67.     InsertMenu(hTempMenu,hierMenu);
  68.     
  69.     out_pMenu->mMinValue=s_numMenu++;    //    MinValue holds the MenuID of the menu to link the
  70.                                         //    resource to. Make sure to increment the s_numMenu
  71.                                         //    so that next time through we get a unique id.
  72.                                     
  73.     out_pMenu->mValue=255;                //    Enable the Menu
  74.     
  75.     out_pMenu->mMaxValue=4+StringWidth(sMenuTitle);    //    Size of the Control Title string.
  76.                                                     //    I like a 2-pixel border around the title
  77.                                                     //    for mousedowns.
  78.                                                     
  79.     //     And make our own Control using PowerPlants StdControl maker
  80.     out_pMenu->InitStdControl(out_pMenu->mControlKind,
  81.                             out_pMenu->mTextTraitsID,
  82.                             sMenuTitle,
  83.                             lRefCon);
  84.     /*****    Plagirism On ********/
  85.     // Popups use the initial values for other purposes. Control Manager
  86.     // determines min/max from the size of the Menu. So now we have to
  87.     // adjust the value, min, and max stored by LControl.
  88.         
  89.     out_pMenu->mValue = ::GetControlValue(out_pMenu->mMacControlH);
  90.     out_pMenu->mMinValue = ::GetControlMinimum(out_pMenu->mMacControlH);
  91.     out_pMenu->mMaxValue = ::GetControlMaximum(out_pMenu->mMacControlH);    
  92.     
  93.     if (0 != out_pMenu->mValue) {
  94.         ::SetControlValue(out_pMenu->mMacControlH, 0);
  95.         out_pMenu->mValue = ::GetControlValue(out_pMenu->mMacControlH);
  96.     }
  97.     /*****    Plagirism Off ********/
  98.     
  99.     return out_pMenu;
  100. }
  101. //    LDynamicPopupMenu::~LDynamicPopupMenu()
  102. //        1/8/95 CS
  103. //            Nothing to do, PowerPlant will clean up
  104. LDynamicPopupMenu::~LDynamicPopupMenu()
  105. {
  106.     ;
  107. }
  108. //    void BroadcastValueMessage()
  109. //        12/14/94 CS - This overrides the LControl:BroadcastValueMessage() member function to 
  110. //            send appropriate messages to the Listener. In your code you should rip this out 
  111. //            and do what is appropriate.
  112. void
  113. LDynamicPopupMenu::BroadcastValueMessage()
  114. {    
  115.     MenuHandle    hMenu=GetMacMenuH();
  116.     BroadcastMessage(mValue, this );
  117. }
  118. //    void LDynamicPopupMenu::AddCMenuItem(
  119. //            const char* in_cpStr)
  120. //        1/8/95 CS
  121. //            Adds an item to a popupmenu. The in_cpStr is a c-style string
  122. void
  123. LDynamicPopupMenu::AddCMenuItem(
  124.     const char* in_csStr)    //    C-String for new menu item this translates to p-string
  125. {
  126.     Str255    menuString;
  127.     c2pstrcpy(in_csStr,menuString);
  128.     AddPMenuItem(menuString);
  129. }
  130. //    void LDynamicPopupMenu::AddPMenuItem(
  131. //            const char* in_cpStr)
  132. //        1/8/95 CS
  133. //            Adds an item to a popupmenu. The in_cpStr is a P-style string
  134. void
  135. LDynamicPopupMenu::AddPMenuItem(
  136.     const StringPtr in_cpStr)    //    P-String for new menu item
  137. {
  138.     MenuHandle    hTmpMenu=GetMacMenuH();
  139.  
  140.     AppendMenu(hTmpMenu,in_cpStr);
  141.     short numMenuItems=CountMItems(hTmpMenu);
  142.     mMinValue=1;
  143.     mMaxValue=numMenuItems;
  144. }
  145. //    void LDynamicPopypMenu::FinishedCreatingMenu(
  146. //            Int32 in_initialItem)
  147. //        1/8/95 CS
  148. //            Don't forget to call this function when your done adding things
  149. //                to your menu.
  150. void
  151. LDynamicPopupMenu::FinishedCreatingMenu(
  152.     Int32 in_initialItem)    //    Initial Menu Item to show
  153. {
  154.     SetStdMinAndMax();
  155.     SetValue(in_initialItem);
  156. }
  157. /**************** Utility Functions **********************/
  158.  
  159. //    void c2pstrcpy(
  160. //            StringPtr out_pStr,
  161. //            const char*    in_pStr)
  162. //        1/8/95 CS
  163. //            Utility function that takes an c-string as input and copies it to out_pStr
  164. //                converting it in the process to a p-string. 
  165. //            NOTE: This is not a world-script ready function & the storage for
  166. //                out_pStr is not allocated it is assumed to be there.
  167. void c2pstrcpy(
  168.     const char*    in_pStr,    //    Input c string
  169.     StringPtr out_pStr)        //    Output p-string
  170. {
  171.     unsigned char     len=0;
  172.     char*            dstLoc=(char*)(out_pStr+1);
  173.     
  174.     while(*in_pStr){
  175.         *dstLoc++=*in_pStr++;
  176.         len++;
  177.         //    You may want to handle this different.
  178.         if (len>255){
  179.             Debugger();
  180.         }
  181.     }
  182.     *out_pStr=len;
  183. }
  184.